/**
 * Created by Administrator on 2019/3/17.
 */
var table;
var t=1;
var parentCode;
$(function () {
    parentCode = locations.ParamString("parentCode");
    table = $("#limitsList");
    intiGrid();
});

var intiGrid = function () {
    var param = queryButtonLimit(parentCode);
    /*var toolbar = [{
        text:'新增',
        iconCls:'icon-add',
        handler:function(){addLimits();}
    },'-',{
        text:'修改',
        iconCls:'icon-edit',
        handler:function(){editLimits();}
    },'-',{
        text:'刷新',
        iconCls:'icon-reload',
        handler:function(){reloadGrid();}
    },'-',{
        text:'删除',
        iconCls:'icon-remove',
        handler:function(){removeLimits();}
    }];*/
    table.treegrid( {
        iconCls : 'icon-remove',
        idField:'id',
        treeField:'limitName',
        striped : true,
        collapsible : false,
        fitColumns :true,
        singleSelect : true,
        animate:true,
        columns : [ [  {
            field : 'id',
            checkbox : true,
            hidden:true
        }, {
            title : '菜单名称',
            field : 'limitName',
            width : 100
        }, {
            title : '是否显示',
            field : 'isShow',
            width : 70,
            formatter:function (value, row, index) {
                if('yes' == value) {
                    return '显示';
                } else {
                    return '不显示';
                }
            }
        }, {
            title : '打开样式',
            field : 'openStyle',
            width : 70,
            formatter:function (value, row, index) {
                if('tabs' == value) {
                    return '标签栏';
                } else if('topWindow' == value) {
                    return '顶层弹窗';
                } else if('messageZone' == value) {
                    return '信息栏';
                } else {
                    return '';
                }
            }
        }, {
            title : '图标',
            field : 'icon',
            width : 70,
            formatter:function (value, row, index) {
                return "<div class=\"easyui-linkbutton\" iconCls='"+value+"'/>"+value+"</div>";
            }
        }, {
            title : 'URL',
            field : 'url',
            width : 150
        }, {
            title : '排序',
            field : 'limitSort',
            width : 80
        }
        ] ],
        toolbar: param,
        onBeforeLoad:function() {
            var param = [];
            var json = postSyncAjax("/findLimitsByParentCode/minus",null).data;
            //console.log(json);
            var limit = json.data;
            for(var i = 0; i<limit.length; i++){
                var iconCls;
                if(limit[i].limitType=="page") {
                    iconCls = "tree-folder";
                } else if(limit[i].limitType=="button") {
                    iconCls = "icon-file";
                } else {
                    iconCls = "icon-file";
                }
                param.push({
                    "id" : limit[i].id,"limitName" : limit[i].limitName,
                    "icon" : limit[i].icon,"url" : limit[i].addr,
                    "state" : "closed","limitSort" : limit[i].limitSort,
                    "t":t,"limitCode":limit[i].limitCode,"isShow":limit[i].isShow,
                    iconCls: iconCls,"openStyle":limit[i].openStyle
                })
            }
            //console.log(param);
            table.treegrid('loadData', param);
            table.treegrid("expandAll");
        },
        onBeforeExpand : function(note) {
            //console.log(note);
            if(note.t==1) {
                 userTreeLoad(note);
            }
        }

    });
}

function userTreeLoad(note) {
    var param = new Array();
    var json = postSyncAjax("/findLimitsByParentCode/"+note.limitCode,null).data;
    var limit = json.data;
    //console.log(limit);
    for(var i = 0; i<limit.length; i++){
        var iconCls;
        if(limit[i].limitType=="page") {
            iconCls = "tree-folder";
        } else if(limit[i].limitType=="button") {
            iconCls = "icon-file";
        } else {
            iconCls = "icon-file";
        }
        param.push({
            "id" : limit[i].id,"limitName" : limit[i].limitName,"icon" : limit[i].icon,
            "url" : limit[i].addr,"state" : "closed","limitSort" : limit[i].limitSort,
            "t":t,"limitCode":limit[i].limitCode,"isShow":limit[i].isShow,
            iconCls: iconCls,"openStyle":limit[i].openStyle
        })
    }
    $('#limitsList').treegrid('append', {
        parent : note.id,
        data : param
    });
    note.t =2;
}

var addLimits = function (uri) {
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    }
    var limitName = rows.limitName;
    var parentId = rows.limitCode;
    var options = {
        reloadGrid:reloadGrid,
        url : uri + '/' + parentId
    };
    showThePage(parentCode, uri, options);
    //openTopWindow(uri+'/'+parentId,limitName+":新增子项权限",600,600,options);
}

var editLimits = function (uri) {
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    }
    var limitName = rows.limitName;
    var id = rows.id;
    var options = {
        reloadGrid:reloadGrid,
        url : uri + '/' + id
    };
    showThePage(parentCode, uri, options);
}

var reloadGrid = function () {
    table.treegrid("reload");
}

var removeLimits = function (uri) {
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    }
    // 查询是否不属于父级权限
    var id = rows.limitCode;
/*    var jsonstr_query = postSyncAjax('/toDeleteLimits/normal/'+id, null);
    if(!jsonstr_query.success) {
        $.messager.alert("消息提示", "权限删除失败："+jsonstr_query.msg, "warning");
        return;
    }*/
    $.messager.confirm("确认对话框","确定要删除该权限信息？",function (r) {
        if(r){
            var jsonstr = postSyncAjax(uri+'/'+id, null);
            //console.log(jsonstr);
            if(jsonstr.success) {
                $.messager.alert("消息提示", "权限删除成功！", "warning");
                table.datagrid("reload");
                /*showDialogWithFunc("模具删除成功("+jsonstr.msg+")！",function () {
                    table.datagrid("reload");
                });*/
            } else {
                $.messager.alert("消息提示", "权限删除失败："+jsonstr.msg, "warning");
                //showDialogWithContent("模具删除失败："+jsonstr.msg);
            }
        }
    });
}